﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Dual licensed under the MIT and GPL licenses:
//  http://www.opensource.org/licenses/mit-license.php
//  http://www.gnu.org/licenses/gpl.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DNA.OpenAPI.Atom
{
    public class AtomEntryBuilder
    {
        private AtomEntry entry;

        public AtomEntry Entry
        {
            get { return entry; }
        }
        
        public AtomEntryBuilder() { entry = new AtomEntry(); }

        public AtomEntryBuilder(string title, string id = "")
        {
            entry = new AtomEntry() { Title = title };
            if (!string.IsNullOrEmpty(id))
                entry.ID = id;
        }

        public void SetBaseInfo(string id, string title, DateTime updated)
        {
            SetID(id);
            SetTitle(title);
            SetLastUpdated(updated);
        }

        public void SetTitle(string title) { entry.Title = title; }

        public void SetID(string id) { entry.ID = id; }

        public void SetAuthor(string name, string uri = "", string email = "")
        {
            entry.Author = CreatePerson(name, uri, email);
        }
        
        public void SetLastUpdated(DateTime date)
        {
            entry.Updated = date;
        }

        public void SetPublished(DateTime date)
        {
            entry.Published = date;
        }

        public void SetSummary(string summary)
        {
            if (!string.IsNullOrEmpty(summary))
                entry.Summary = summary;
        }

        public void SetContent(string content, string type = "text/html", string src = "")
        {
            if (!string.IsNullOrEmpty(content) && !string.IsNullOrEmpty(src))
            {
                entry.Content = new AtomContent() { ContentType = type, };
                if (!string.IsNullOrEmpty(content))
                    entry.Content.Text = content;
                if (!string.IsNullOrEmpty(src))
                    entry.Content.SourceUrl = src;
            }
        }

        public AtomLink AddLink(string href, string title = "", string rel = "", string type = "", string hreflang = "", string length = "")
        {
            var link = new AtomLink() { NavigateUrl = href };
            if (!string.IsNullOrEmpty(title)) link.Text = title;
            if (!string.IsNullOrEmpty(rel)) link.Rel = rel;
            if (!string.IsNullOrEmpty(type)) link.Type = type;
            if (!string.IsNullOrEmpty(hreflang)) link.NavigateUrlLanguage = hreflang;
            if (!string.IsNullOrEmpty(length)) link.Length = length;
            this.AddLink(link);
            return link;
        }

        public void AddLink(AtomLink link)
        {
            if (entry.Links == null) entry.Links = new List<AtomLink>();
            entry.Links.Add(link);
        }

        public void AddLinks(IEnumerable<AtomLink> links)
        {
            if (entry.Links == null) entry.Links = new List<AtomLink>();
            entry.Links.AddRange(links);
        }

        public void AddCategories(IEnumerable<AtomCategory> categories)
        {
            if (entry.Categories == null)
                entry.Categories = new List<AtomCategory>();
            entry.Categories.AddRange(categories);
        }

        public AtomCategory AddCategory(string term, string label = "", string schema = "")
        {
            var cat = new AtomCategory() { Term = term };
            if (!string.IsNullOrEmpty(label)) cat.Text = label;
            if (!string.IsNullOrEmpty(schema)) cat.Scheme = schema;
            this.AddCategory(cat);
            return cat;
        }

        public void AddCategory(AtomCategory category)
        {
            if (entry.Categories == null)
                entry.Categories = new List<AtomCategory>();
            entry.Categories.Add(category);
        }

        public void AddContributors(IEnumerable<AtomPersonConstruct> contributors)
        {
            if (entry.Contributors == null) entry.Contributors = new List<AtomPersonConstruct>();
            entry.Contributors.AddRange(contributors);
        }

        public AtomPersonConstruct AddContributor(string name, string uri = "", string email = "")
        {
            var person = CreatePerson(name, uri, email);
            this.AddContributor(person);
            return person;
        }

        private AtomPersonConstruct CreatePerson(string name, string uri = "", string email = "")
        {
            var person = new AtomPersonConstruct() { Name = name };
            if (!string.IsNullOrEmpty(uri)) person.Uri = uri;
            if (!string.IsNullOrEmpty(email)) person.Email = email;
            return person;
        }

        public void AddContributor(AtomPersonConstruct person)
        {
            if (entry.Contributors == null) entry.Contributors = new List<AtomPersonConstruct>();
            entry.Contributors.Add(person);
        }
    }
}
